//
//  MyStoreViewController.m
//  Manager
//
//  Created by Mr.Lee on 2018/4/16.
//  Copyright © 2018年 Reabam. All rights reserved.
//

#import "MyStoreViewController.h"
#import "ClassificationSearchView.h"
#import "ShopKeepingCell.h"         // cell
//#import "StoreInforViewController.h"  // 门店信息
#import "StaffCompListModel.h"     // 门店列表model
#import "TSFooterRefresh.h"
//#import "StoreInspectionViewController.h"  // 巡店
#import "ClerkManagerViewController.h"
#import "StoreSearchViewController.h"
#define KPCellHeight 109           // cell高度
@interface MyStoreViewController () <UITextFieldDelegate ,UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, weak) UITableView *tableV;
@property (nonatomic, strong) NSMutableArray *modelArray;
@property (nonatomic, weak) UILabel *headerTitleLabel;
@end

@implementation MyStoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"我的商户";
    self.modelArray = [NSMutableArray array];
    
    UIImage *img=[UIImage imageNamed:@"sousuo"];
    UIImage *tinted = [img rt_tintedImageWithColor:[UIColor whiteColor]];
    UIButton *searchBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 0.0, 60/2, 60/2)];
    [searchBtn setImage:tinted forState:UIControlStateNormal];
    [searchBtn setImage:tinted forState:UIControlStateHighlighted];
    [searchBtn addTarget:self action:@selector(searchBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* searchBtnItem = [[UIBarButtonItem alloc] initWithCustomView:searchBtn];
    self.navigationItem.rightBarButtonItem = searchBtnItem;
    
    
    [self setUpTableView];
    // 加载数据
    [self requestData];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(UPDATECOMPANYLIST) name:@"UPDATECOMPANYLIST" object:nil];
    
}
- (void)UPDATECOMPANYLIST{
    [self requestData];
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UPDATECOMPANYLIST" object:nil];
}

- (void)searchBtnClick:(UIButton *)sender{
    StoreSearchViewController *vc = [[StoreSearchViewController alloc] init];
    vc.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)setUpTableView{
    
    UIView *headerView = [[UIView alloc] init];
    headerView.frame = CGRectMake(0, 0, ScreenWidth, 40);
    headerView.backgroundColor = lanse1;
    UILabel *headerTitleLabel = [[UILabel alloc] init];
    self.headerTitleLabel = headerTitleLabel;
    headerTitleLabel.frame = CGRectMake(10, 0, ScreenWidth-30, 40);
    headerTitleLabel.font = [UIFont systemFontOfSize:14.0];
    headerTitleLabel.textColor = [UIColor colorWithHexString:@"0880c6"];
    [headerView addSubview:headerTitleLabel];
    [self.view addSubview:headerView];
    
    UITableView *tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, headerView.bottom, ScreenWidth, ScreenHeight-64-40) style:UITableViewStylePlain];
    tableV.backgroundColor = [UIColor whiteColor];
    tableV.dataSource = self;
    tableV.delegate = self;
    tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableV = tableV;
    UIView *emputyView = [[UIView alloc] init];
    tableV.tableFooterView = emputyView;
    [self.view addSubview:tableV];
    
}


#pragma mark -- 网络请求
- (void)requestData{
 
    NSString *Token = [UserDefaults objectForKey:@"tokenId"];
    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setValue:Token forKey:@"tokenId"];
//    [params setValue:@"1" forKey:@"sword"];
    NSLog(@"-----params---- %@", params);
    [MBProgressHUD showIndeterminateHudSub:@"加载中，请稍候。。。" delay:30 view:self.view];
    [HttpRequest requestData:@"manage/findCompanyList" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        NSLog(@"OrderListllllll====%@",severDataDict.dataDict);
        if (isSuccess) {
            [self.modelArray removeAllObjects];
            [MBProgressHUD hideProgressHUDInternal:self.view];
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                NSMutableArray *dataArr = [severDataDict.dataDict objectForKey:@"compDatas"];
                if (dataArr.count > 0) {
                    for (NSDictionary* dicc in dataArr ){
                        StaffCompListModel *model = [StaffCompListModel mj_objectWithKeyValues:dicc];
                        [self.modelArray addObject:model];
                    }
                    [self.tableV reloadData];
                    
                    // 获取总数
                    NSString *compQty=[severDataDict.dataDict objectForKey:@"compQty"];
                    self.headerTitleLabel.text = [NSString stringWithFormat:@"共 %@ 个商户", compQty];

                }
            
            } else {
                [MBProgressHUD hideProgressHUDInternal:self.view];
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [MBProgressHUD showTextHudSub:requestResultStr view:self.view];
                
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                    LoginViewController *loginVC = [[LoginViewController alloc] init];
                    [self.navigationController pushViewController:loginVC animated:YES];
                }
            }
        } else {
            [MBProgressHUD hideProgressHUDInternal:self.view];
            [MBProgressHUD showTextHudSub:severDataDict.resultStr view:self.view];
        }
    }];
    
}
#pragma mark - TableView DateSource
//每行的高度
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return  KPCellHeight;
}
// 返回多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
// 每组多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}
// 返回每行cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    StaffCompListModel *model = self.modelArray[indexPath.row];
    static NSString *CellIdentifier = @"cell";
    ShopKeepingCell *cell = ( ShopKeepingCell *)[tableView dequeueReusableCellWithIdentifier: CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"ShopKeepingCell" owner:nil options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = model;
    return cell;
    
}


#pragma mark - TableView Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 门店信息
    StaffCompListModel *model = self.modelArray[indexPath.row];
    ClerkManagerViewController *storeInforVC = [[ClerkManagerViewController alloc] init];
    storeInforVC.companyId = model.companyId;
    storeInforVC.titleName = model.companyName;
    storeInforVC.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController pushViewController:storeInforVC animated:YES];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
@end
